package com.zh.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.BaseResponse;
import com.zh.common.ErrorCode;
import com.zh.common.ResultUtils;
import com.zh.domain.dto.attache.DeleteAttacheRequest;
import com.zh.domain.dto.attache.PageAttacheRequest;
import com.zh.domain.vo.attache.AttachesVo;
import com.zh.exception.ThrowUtils;
import com.zh.service.AttachesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "AttacheController", value = "附件控制器")
@RestController
@RequestMapping("/attache")
@Slf4j
@SaCheckLogin
public class AttacheController {


    @Resource
    private AttachesService attachesService;

    @PostMapping("/add")
    @ApiOperation(value = "addAttache", notes = "添加附件")
    @ApiImplicitParam(name = "Content-Type", value = "multipart/form-data", required = true, dataType = "string", paramType = "header")
    public BaseResponse<Boolean> addAttache(@RequestParam("type") Integer type,
                                            @RequestParam("typeId") Integer typeId,
                                            @RequestParam("fileName") String fileName,
                                            @RequestPart("file") MultipartFile file
    ) {
        Boolean result = attachesService.add(type, typeId, fileName, file);
        return ResultUtils.success(result);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "deleteAttache", notes = "删除附件")
    public BaseResponse<Boolean> deleteAttache(@RequestBody DeleteAttacheRequest deleteAttacheRequest) {
        ThrowUtils.throwIf(null == deleteAttacheRequest, ErrorCode.PARAMS_ERROR);
        Boolean result = attachesService.deleteAttache(deleteAttacheRequest);
        return ResultUtils.success(result);
    }

    @PostMapping("/page")
    @ApiModelProperty(value = "pageAttaches", notes = "分页查询附件")
    public BaseResponse<Page<AttachesVo>> pageAttaches(@RequestBody PageAttacheRequest pageAttacheRequest) {
        ThrowUtils.throwIf(null == pageAttacheRequest, ErrorCode.PARAMS_ERROR);
        Page<AttachesVo> pageResult = attachesService.pageAttache(pageAttacheRequest);
        return ResultUtils.success(pageResult);
    }
}
